#-*- coding: utf-8 -*-
from odoo import models, fields, api
from odoo.exceptions import Warning
from odoo.tools.translate import _
import odoo.addons.decimal_precision as dp
from datetime import datetime


class mrp_bom(models.Model):
    _inherit = 'mrp.bom'

    product_id = fields.Many2one(
        'product.product', 'Product Variant',
        domain="[('type', 'in', ['product', 'consu'])]",
        help="If a product variant is defined the BOM is available only for this product.")


    @api.onchange('product_id')
    def _onchange_product_id(self):
        if self.product_id:
            self.product_tmpl_id = self.product_id.product_tmpl_id.id

# 尽量保证原功能正常使用。改了字段定义后，选择产品模板，产品的选择不会有过滤。所以添加此方法

    # @api.onchange('product_tmpl_id')
    # def _onchange_product_tmpl_id(self):
    #     if self.product_tmpl_id:
    #         result = {}
    #         #发现有bug.新建bom后,再新建,产品变形不能选了.所以取消掉
    #         # result['domain'] = {
    #         #     'product_id': [
    #         #         ('product_tmpl_id', '=', self.product_tmpl_id.id), ('type', 'in', ['product', 'consu'])
    #         #     ]
    #         # }
    #         return result